package ThreadDemo;
/*多个线程同时修改同一个变量带来的线程安全问题*/
class Counter{
    public int a=0;
    synchronized public void increase() {
        a++;
    }

}

public class ThreadDemo14 {
    public static void main(String[] args) throws InterruptedException {
        Counter counter=new Counter();
        Thread t1 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    counter.increase();
                }
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    counter.increase();
                }
            }
        };
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(counter.a);
    }
}
